package com.youfan.book.controller;

import com.youfan.book.domain.Bookrack;
import com.youfan.book.domain.Booktype;
import com.youfan.book.service.BookrackService;
import com.youfan.book.service.BooktypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("bookrack")
@Controller
public class BookrackController {

    @Autowired
    private BooktypeService booktypeService;
    @Autowired
    private BookrackService bookrackService;

    @GetMapping("booktype")
    public String booktypeList(Model model) {
        model.addAttribute("list", booktypeService.list());
        return "/bookrack/bookType";
    }

    @GetMapping("booktype_add")
    public String booktypeAdd() {
        return "/bookrack/bookType_add";
    }

    @PostMapping("booktype_add_do")
    public String booktypeAddDo(Booktype booktype) {
        booktypeService.save(booktype);
        return "redirect:/bookrack/booktype";
    }

    @GetMapping("booktype_delete")
    public String booktypeDelete(Long id) {
        booktypeService.removeById(id);
        return "redirect:/bookrack/booktype";
    }

    @GetMapping("booktype_modify")
    public String booktypeModify(Long id, Model model) {
        Booktype booktype = booktypeService.getById(id);
        model.addAttribute("booktype", booktype);
        return "/bookrack/bookType_Modify";
    }

    @PostMapping("booktype_modify_do")
    public String booktypeModifyDo(Booktype booktype) {
        booktypeService.updateById(booktype);
        return "redirect:/bookrack/booktype";
    }

    @GetMapping("bookcase")
    public String bookcase(Model model) {
        model.addAttribute("list", bookrackService.list());
        return "bookrack/bookcase";
    }

    @GetMapping("bookcase_add")
    public String bookcaseAdd() {
        return "/bookrack/bookcase_add";
    }

    @PostMapping("bookcase_add_do")
    public String bookcaseAddDo(Bookrack bookrack) {
        bookrackService.save(bookrack);
        return "redirect:/bookrack/bookcase";
    }

    @GetMapping("bookcase_modify")
    public String bookcaseModify(Long id, Model model) {
        model.addAttribute("item", bookrackService.getById(id));
        return "/bookrack/bookCase_Modify";
    }

    @PostMapping("bookcase_modify_do")
    public String bookcaseModifyDo(Bookrack bookrack) {
        bookrackService.updateById(bookrack);
        return "redirect:/bookrack/bookcase";
    }

    @GetMapping("bookcase_delete")
    public String bookcaseDelete(Long id) {
        bookrackService.removeById(id);
        return "redirect:/bookrack/bookcase";
    }
}
